#include "mainwindow.h"
#include "sshsession.h"
#include <QApplication>
#include <QMessageBox>
#include <QSettings>
#include <QTranslator>
#include <QLibraryInfo>
#include <QDebug>
#include <QFile>
#include <QTextStream>

// 日志处理函数
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    Q_UNUSED(context)

    QString txt;
    switch (type) {
    case QtDebugMsg:
        txt = QString("Debug: %1\n").arg(msg);
        break;
    case QtInfoMsg:
        txt = QString("Info: %1\n").arg(msg);
        break;
    case QtWarningMsg:
        txt = QString("Warning: %1\n").arg(msg);
        break;
    case QtCriticalMsg:
        txt = QString("Critical: %1\n").arg(msg);
        break;
    case QtFatalMsg:
        txt = QString("Fatal: %1\n").arg(msg);
        break;
    }

    // 输出到调试控制台
    QTextStream(stdout) << txt << Qt::endl;

    // 输出到文件
    QFile outFile("sshtool.log");
    if (outFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
        QTextStream ts(&outFile);
        ts << txt << Qt::endl;
    }

    if (type == QtFatalMsg) {
        abort();
    }
}

int main(int argc, char *argv[])
{
    // 设置高DPI支持 - 必须在QApplication创建之前
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    // 设置应用程序信息
    QApplication::setApplicationName("SshTool");
    QApplication::setApplicationVersion("1.0.0");
    QApplication::setOrganizationName("SshTool");
    QApplication::setOrganizationDomain("sshtool.com");

    QApplication app(argc, argv);

    // 注册SessionState类型用于跨线程信号传递
    qRegisterMetaType<SshSession::SessionState>("SessionState");

    // 安装日志处理器
    qInstallMessageHandler(messageHandler);

    // 加载应用程序翻译
    QTranslator translator;
    if (translator.load(QLocale(), QLatin1String("sshtool"), QLatin1String("_"), QLatin1String(":/translations"))) {
        app.installTranslator(&translator);
    }

    // 加载Qt系统翻译
    QTranslator qtTranslator;
    if (qtTranslator.load(QLocale(), QLatin1String("qtbase"), QLatin1String("_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
        app.installTranslator(&qtTranslator);
    }

    // 检查单例运行
    QSettings settings;
    bool allowMultipleInstances = settings.value("settings/allowMultipleInstances", false).toBool();

    if (!allowMultipleInstances) {
        // 这里可以添加单例检查逻辑
        // 例如使用QLocalSocket检查是否已有实例运行
    }

    // 创建主窗口
    MainWindow mainWindow;

    // 恢复窗口状态
    settings.beginGroup("MainWindow");
    mainWindow.restoreGeometry(settings.value("geometry").toByteArray());
    mainWindow.restoreState(settings.value("windowState").toByteArray());
    bool startMaximized = settings.value("maximized", false).toBool();
    bool startMinimized = settings.value("minimized", false).toBool();
    settings.endGroup();

    // 显示窗口
    if (!startMinimized) {
        if (startMaximized) {
            mainWindow.showMaximized();
        } else {
            mainWindow.show();
        }
    }

    // 执行应用程序
    int result = app.exec();

    // 保存设置
    settings.beginGroup("MainWindow");
    settings.setValue("geometry", mainWindow.saveGeometry());
    settings.setValue("windowState", mainWindow.saveState());
    settings.setValue("maximized", mainWindow.isMaximized());
    settings.endGroup();

    return result;
}
